#include "BatchCheckOutRequestModel.h"
using namespace MesModel;

BatchCheckOutRequestModel::BatchCheckOutRequestModel(QString factoryNo, QString transId, QDateTime enventTime, QString eventUser, QString machineNo, QString batch, QList<BatchCheckOutRequestPcuInfo> pcuInfo)
    :factoryNo(factoryNo),
    transId(transId),
    eventTime(eventTime),
    eventUser(eventUser),
    machineNo(machineNo),
    batch(batch),
    pcuInfo(pcuInfo)

{

}

void BatchCheckOutRequestModel::fromJson(const QJsonObject &json)
{
    factoryNo = json["factoryNo"].toString();
    transId = json["transId"].toString();
    eventTime = QDateTime::fromString(json["eventTime"].toString());
    eventUser = json["eventUser"].toString();
    machineNo = json["machineNo"].toString();
    batch = json["batch"].toString();

    const QJsonArray pcu_info_array = json["pcuInfo"].toArray();
    for(const auto& item:pcu_info_array)
    {
        BatchCheckOutRequestPcuInfo item_info;
        item_info.fromJson(item.toObject());
    }
}

QJsonObject BatchCheckOutRequestModel::toJson() const
{
    QJsonObject json;
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    json["factoryNo"] = factoryNo;
    json["transId"] = QString("%1%2").arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS")).arg(randomNumber);
    json["eventTime"] = eventTime.toString("yyyy-mm-dd hh:mm:ss");
    json["eventUser"] = eventUser;
    json["batch"] = batch;

    QJsonArray pcu_info_array;
    for(const auto& item:pcuInfo)
    {
        pcu_info_array.append(item.toJson());
    }
    json["pcuInfo"] = pcu_info_array;
    return json;
}



BatchCheckOutRequestPcuInfo::BatchCheckOutRequestPcuInfo(QString locationNo, QString pcu, QString gear, QList<QString> ngCodeList)
    :locationNo(locationNo),
    pcu(pcu),
    gear(gear),
    ngCodeList(ngCodeList)
{
}

void BatchCheckOutRequestPcuInfo::fromJson(const QJsonObject &json)
{
    locationNo = json["locationNo"].toString();
    pcu = json["pcu"].toString();

    const QJsonArray ng_code_array = json["ngCodeList"].toArray();
    for(const auto& ng_code:ng_code_array)
    {
        ngCodeList.append(ng_code.toString());
    }

}


QJsonObject BatchCheckOutRequestPcuInfo::toJson() const
{
    QJsonObject json;
    json["locationNo"] = locationNo;
    json["pcu"] = pcu;
    QJsonArray ng_code_array;
    for(const auto& ng_code:ngCodeList)
    {
        ng_code_array.append(ng_code);
    }
    json["ngCodeList"] = ng_code_array;
    return json;

}
